package com.carmanage.dao.impl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.carmanage.dao.RoleDao;
import com.carmanage.domain.Action;
import com.carmanage.domain.Page;
import com.carmanage.domain.Role;


@Repository
public class RoleDaoImpl implements RoleDao {
    
	private SessionFactory sessionFactory;
	


    @Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Override
	public List<Role> findAllRole() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Role");
		List<Role> roles = query.list();
		session.close();
		return roles;
	}

	@Override
	public Role findRoleByRoleId(int roleid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Role role = (Role) session.get(Role.class,roleid);
		session.close();
		return role;
	}
	@Override
	public int addRole(String rolename,String creatdate) {
		// TODO Auto-generated method stub
       Session session = sessionFactory.openSession();
       Role role = new Role();
       try{
    	   role.setRolename(rolename);
    	   role.setCreatdate(creatdate);
    	   session.save(role);
    	   session.close();
    	   return 1;
       }catch(HibernateException e){
    	   session.close();
    	   return 0;
       }
       
	}
	@Override
	public int updateRole(String rolename, List<Integer> ActionIds) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Role role = new Role();
		Action action = new Action();
		role.setRolename(rolename);
		for(int i=0;i<ActionIds.size();i++){
            action = (Action) session.get(Action.class,ActionIds.get(i));
            action.getRoles().add(role);
		}
		session.close();
		return 1;
	}
	@Override
	public Role getRoleByRoleId(int roleid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		Role role = new Role();
		role = (Role) session.get(Role.class,roleid);
		session.close();
		return role;
	}
	@Override
	public List<Role> findRoleByPage(Page rolePage) {
		// TODO Auto-generated method stub
		   Session session = sessionFactory.openSession();
		    Query query = session.createQuery("from Role");
		    //设置每页显示的个数,即多大结果
		    query.setMaxResults(rolePage.getPageSize());
		    //设置起点行号
		    query.setFirstResult(rolePage.getPageStartRow());
			List<Role> rolesByPage = query.list();
			return rolesByPage;
	}
	@Override
	public int findAllRoleRecord() {
		// TODO Auto-generated method stub
		   Session session = sessionFactory.openSession();
	        Query query = session.createQuery("select count(*) from Role r");
			Long  middleValue = (Long) query.uniqueResult();
			int AllRoleRecord = Integer.valueOf(middleValue.toString());
			session.close();
	        return AllRoleRecord;
	}

	@Override
	public int updateRole(List<Integer> listAction, int roleid) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.openSession();
		int i =0;
		Action action = new Action();
		try{
		Role role = (Role) session.get(Role.class,roleid);
		System.out.println("roleid="+roleid);
		for(int k=0;k<listAction.size();k++){
			System.out.println("listAction.get(k)="+listAction.get(k));
			action = (Action) session.get(Action.class,listAction.get(k));
            System.out.println("action"+action);
			action.getRoles().add(role);
			session.save(role);
            session.save(action);
            session.flush();
		}
		 session.close();
		 return 1;
		}catch(HibernateException e){
			e.printStackTrace();
			session.close();
			return i;
		}
		
	}
	
	
}
